//
//  HMStatusPictureView.swift
//  HMVVeibo23
//
//  Created by smile on 17/2/4.
//  Copyright © 2017年 apple. All rights reserved.
//

import UIKit
import SDWebImage
private let statusPictureCellId = "statusPictureCellId"
private let itemMargin: CGFloat = 5

private let itemWH = (SCREENWIDTH - itemMargin * 2 - 20)/3

class HMStatusPictureView: UICollectionView {
    
    var pictureUrls: [HMPictureModel]?{
        didSet{

            let size = dealPictureViewSize(count: pictureUrls?.count ?? 0)
            
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            //  设置单张图片的条目的大小
            let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
            if pictureUrls?.count == 1 {
                
                //  重写设置itemSize
                flowLayout.itemSize = size
            }else {
                flowLayout.itemSize = CGSize(width: itemWH, height: itemWH)
            }
            
            //  保证配图的大小和itemSize大小同步
            self.layoutIfNeeded()
            
            reloadData()
        }
    }
    
    func dealPictureViewSize(count: Int) -> CGSize{
        
        if count == 1 {
            // 获取单张图片地址
            if let imgUrlString = pictureUrls?.first?.thumbnail_pic {
                // 通过地址获取image
                if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: imgUrlString) {
                    // 获取image 的size
                    var imageW = image.size.width
                    var imageH = image.size.height
                    
                    if imageW < 80 {
                        imageW = 80
                    }
                    
                    if imageH > 150 {
                        imageH = 150
                    }
                    
                    return CGSize(width: imageW, height: imageH)
                }
            }
        }
        
        
        
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1) / 3 + 1
        
        let w = (CGFloat(col) * itemWH) + CGFloat(col - 1)*itemMargin
        let h = (CGFloat(row) * itemWH) + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: w, height: h)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: itemWH, height: itemWH)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
         setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
//        backgroundColor = randomeColor()
        
        register(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: statusPictureCellId)
        self.dataSource = self
    }
}

extension HMStatusPictureView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureUrls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: statusPictureCellId, for: indexPath) as! HMStatusPictureViewCell
        cell.pictureUrl = pictureUrls![indexPath.item]
        return cell
    }
}



class HMStatusPictureViewCell: UICollectionViewCell {
    
    var pictureUrl: HMPictureModel?{
        didSet{
            pictureImageView.hm_setImage(imgString: pictureUrl?.thumbnail_pic)
//           pictureImageView.hm_setImage(urlString: pictureUrl?.thumbnail_pic)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
//        backgroundColor = randomeColor()
        
        contentView.addSubview(pictureImageView)
        
        pictureImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView
            )
        }
    }
    
    private lazy var pictureImageView: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    
}
